package com.hantanger.weblog.dao;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.hantanger.weblog.model.Archive;
import com.hantanger.weblog.util.DaoHelper;

public class ArchiveDao {
	
	public void insert(Archive archive) {
		update(archive);
	}

	public void update(Archive archive) {
		DaoHelper.save(archive);
	}

	public void delete(Long id) {
		DaoHelper.delete(Archive.class, id);
	}

	public Archive getArchiveById(Long id) {
		return DaoHelper.get(Archive.class, id);
	}

	public List<Archive> getAllArchives() {
		List<Archive> archives = DaoHelper.getLoader().type(Archive.class).list();
		Collections.sort(archives, new Comparator<Archive>(){
			@Override
			public int compare(Archive a1, Archive a2) {
				return a1.getId() < a2.getId() ? 1 :-1;
			}
		});
		
		return archives;
	}

}
